import type { App, Plugin } from 'vue'
import constant from '@/utils/settings'

// 把路径转换成驼峰命名
export const pathToCamel = (path: string): string => {
	return path.replace(/\/(\w)/g, (_all, letter) => letter.toUpperCase())
}

// 是否外链
export const isExternalLink = (url: string): boolean => {
	return /^(https?:|\/\/|http?:|\/\/|^{{\s?apiUrl\s?}})/.test(url)
}

// 替换外链参数
export const replaceLinkParam = (url: string): string => {
	return url.replace('{{apiUrl}}', constant.apiUrl)
}

// 全局组件安装
export const withInstall = <T extends Record<string, any>>(component: T, alias?: string) => {
	const comp = component as any
	/*comp.install = (app: App) => {
		app.component(comp.name || comp.displayName, component)
		if (alias) {
			app.config.globalProperties[alias] = component
		}
	}
	return component as T & Plugin */
    comp.install = (app: App) => {
    const name = comp.name || comp.displayName

    if (name) {
      app.component(name, component)
    }

    if (alias && name) {
      app.config.globalProperties[alias] = component
    }
  }

  return comp
}